<?php
/**[前后公用控制器]
 * @Author: chen
 * @Date:   2014-08-17 18:00:43
 * @Last Modified by:   happy
 * @Last Modified time: 2015-03-14 17:30:53
 */
class CommonAction extends ExtendAction{


	
	public $categoryService;
	public $articleService;

	public $aid;
	public $cid;

	private $cache;

	public function _initialize()
	{
		$this->categoryService = D('Category');
		$this->articleService  = D(HOME_NAME.'/Article','Service');
		$this->cache  = S('category');

		$this->aid = I('get.aid');
		$this->cid = I('get.cid');
	}

	/**
	 * [index 首页]
	 * @return [type] [description]
	 */
	public function index()
	{
		$base = $this->base();
		$this->assign('cms',$base);
		$this->display();
	}

	/**
	 * [cover 封面]
	 * @return [type] [description]
	 */
	public function cover()
	{
		// 基本信息
		$base = $this->base();
		// 栏目信息
		$cate = $this->category();

		// 面包屑导航
		$bread = $this->articleService->get_bread($this->cid,0);
		// 头部title
		$title = $this->articleService->get_title($this->cid,0);
		
		// 数据合并
		$cms = array_merge($base,$cate,$bread,$title);
		// 分配
		$this->assign('cms',$cms);



		// 模板
		$this->display($cms['default_tpl']);
	}

	/**
	 * [lists 列表页]
	 * @return [type] [description]
	 */
	public function lists()
	{
		// 基本信息
		$base = $this->base();
		// 栏目信息
		$cate = $this->category();

		// 面包屑导航
		$bread = $this->articleService->get_bread($this->cid,0);
		// 头部title
		$title = $this->articleService->get_title($this->cid,0);
		
		// 数据合并
		$cms = array_merge($base,$cate,$bread,$title);
		// 分配
		$this->assign('cms',$cms);


		// 模板
		$this->display($cms['list_tpl']);
	}

	/**
	 * [view 详细页]
	 * @return [type] [description]
	 */
	public function view()
	{
		// 基本信息
		$base = $this->base();
		// 栏目信息
		$cate = $this->category();



		
		/***详细信息***/
		$aid = $this->aid;
		if(!$aid)
			$aid = $this->articleService->find_aid_by_category_cid($this->cid);
		if(!$aid)
			$this->redirect('Home/Index/index');

		$modelAll = S('model');
		$data['table'] = 'article_'.$modelAll[$cate['mid']]['name'];
		$data['aid']   = $aid;
 		
 		// 所有数据（主表+附表）
		$article = $this->articleService->find_one_by_table_aid($data);

		// 图集
		$article['pics'] = D('Pic','Service')->find_all_by_article_aid($aid);

		$cate['page_description'] = $article['description']?$article['description']:$cate['page_description'];
		$cate['page_keywords'] 	  = $article['keywords']?$article['keywords']:$cate['page_keywords'];

		// 面包屑导航
		$bread = $this->articleService->get_bread($this->cid,$this->aid);
		// 头部title
		$title = $this->articleService->get_title($this->cid,$this->aid);

		// 数据合并
		$cms = array_merge($base,$cate,$article,$bread,$title);
		// 数据合并
		$this->assign('cms',$cms);

		// 增加点击次数
		D('Article')->where(array('aid'=>$cms['aid']))->setInc('click',1);



		// 模板
		$this->display($cms['view_tpl']);
	}



	/**
	 * [base 基本信息]
	 * @return [type] [description]
	 */
	public function base()
	{
		// 网站名称
		$base['page_title']  		= C('cfg_name');
		// 网站描述
		$base['page_description']   = C('cfg_description');
		// 关键词
		$base['page_keywords']  	= C('cfg_keywords');



		return $base;
	}

	/**
	 * [category 栏目信息]
	 * @return [type] [description]
	 */
	public function category()
	{
		/***当前栏目信息***/
		$cid           = $this->cid;
		$category      = $this->cache[$cid];
		// 当前栏目cid
		$cate['cid']   = $category['cid'];
		// 当前栏目名称
		$cate['cname'] = $category['cname'];
		// 描述
		$cate['page_description'] = $category['description']?$category['description']:C('cfg_description');
		// 关键词
		$cate['page_keywords'] 	  = $category['keywords']?$category['keywords']:C('cfg_keywords');

		// 封面模板
		$cate['default_tpl']  = $category['default_tpl'];
		// 列表页模板
		$cate['list_tpl']     = $category['list_tpl'];
		// 详细也模板
		$cate['view_tpl']     = $category['view_tpl'];
		// 模型mid
		$cate['mid']     	  = $category['model_mid'];
		$cate['catPic'] 	  = $category['pic']?__ROOT__.'/'.$category['pic']:__ROOT__.'/Data/Public/images/default.gif';

		/***顶级栏目信息***/
		$parentCids    	  = $this->categoryService->get_parent_cid($cid);
		$topCid       	  = $parentCids[count($parentCids)-1];
		$top              = $this->cache[$topCid];
		// 顶级栏目cid
		$cate['topCid']   = $topCid;
		// 顶级栏目名称
		$cate['topCname'] = $top['cname'];

		
		return $cate;


	}

	/**
	 * [msg 留言]
	 * @return [type] [description]
	 */
	public function msg()
	{
		if(!IS_POST)
			_404('链接错误',__ROOT__);
		$db = D('Feedback','Logic');
		if(!$db->add_feedback())
			$this->error($db->getError());
		$this->success(L('lang_live_feedback_success'));
	}

	/**
	 * [_empty 空操作]
	 * @return [type] [description]
	 */
	public function _empty()
	{
		halt('页面不存在');
	}

	
}